"use strict"
import api from './_api'


export var localToken = async()=>{
  if(!('token' in localStorage)){
    let tokenRes = await api(`/auth/get_token`,{})
    return localStorage.token = tokenRes.token
  }else{
    return localStorage.token
  }
}


export var _getThis  = (_exports,Table)=>{
    Object.keys(exports)
    .filter(k => !k.startsWith('_') && !(k in _exports) && typeof(exports[k]) === 'function')
    .map(k => {
        _exports[k] = function(...args){
            return exports[k](_exports,...args)
        }
    })
}
export var fetch = async (ithis,{filter={},orderBy='createAt',limit=5,startPos=0})=>{
  var _Res = await api(`/${ithis.Table}/fetch?token=${await localToken()}`,{filter,orderBy,limit,startPos})
  return _Res
}
export var getByNum = async (ithis,cnum)=>{
  var _Res = await api(`/${ithis.Table}/getByNum?token=${await localToken()}`,{cnum:cnum})
  return _Res
}
export var updateByNum = async (ithis,cnum,options)=>{
  var _Res = await api(`/${ithis.Table}/updateByNum?token=${await localToken()}`,{cnum:cnum,item:options})
  return _Res

}
export var addItem = async (ithis,item)=>{
  var _Res =await api(`/${ithis.Table}/addItem?token=${await localToken()}`,{item})
  return _Res
}
export var deleteByNum = async (cnum)=>{
  var _Res = await api(`/${ithis.Table}/deleteByNum?token=${await localToken()}`,{cnum:num,item:options})
  return _Res
}
